package com.css.dzj.dzjwgr.common.util.Restful;

import com.css.dzj.dzjwgr.common.util.ServiceUtil;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


public class RestfulUtil {

    public static Map sendRestful(HttpServletRequest request, Map<String, String> map, String url, HttpMethod method){
        RestTemplate restTemplate = StaticAPI.getRestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
       /* HttpSession  session= request.getSession();
        if (session.getAttribute("token")!=null) {
            headers.set("token",session.getAttribute("token").toString());
        }*/
        String token = ServiceUtil.getToken(request);
        if (token!=null) {
            headers.set("token",token);
        }
        //将请求头部和参数合成一个请求
        HttpEntity<Map<String,String>> requestEntity = new HttpEntity<>(map,headers);
        ResponseEntity<Map> result=  restTemplate.exchange(url,method,requestEntity,Map.class);
        Map data = (Map) result.getBody();
        return data;
    }
}
